#include "CMainMenuState.h"
#include "../CGame.h"
#include "CGamePlayState.h"
#include "OptionsState.h"
#include "CCreditsState.h"
#include "CHowToPlay.h"
#include "../BitMapFont.h"
// Wrappers
#include "..\Wrappers\CSGD_Direct3D.h"
#include "..\Wrappers\CSGD_DirectInput.h"
#include "..\Wrappers\CSGD_FModManager.h"
#include "..\Wrappers\CSGD_TextureManager.h"


CMainMenuState::CMainMenuState()
{
	_nBKGGImage = -1;
	m_bPressStart = true;
}

void CMainMenuState::Enter()
{
	m_nAlphaStart = 0;
	m_nScroll = CGame::GetInstance()->GetScnWidth() *.5;
	m_nCursorIndex = 0;
	m_nCursorID = CSGD_TextureManager::GetInstance()->LoadTexture("resource/SGD_MenuCursor.png");
	_nBKGGImage = CSGD_TextureManager::GetInstance()->LoadTexture("resource/DHD_BKG.png");
	m_nLogo = CSGD_TextureManager::GetInstance()->LoadTexture("resource/TitleLogo2.png");
}

bool CMainMenuState::Input(void)
{
	CSGD_DirectInput* pDI = CSGD_DirectInput::GetInstance();

	if( !m_bPressStart )
	{
		if(pDI->KeyPressed(DIK_RIGHT) || pDI->JoystickDPadPressed(DIR_RIGHT))
		{

			if(m_nCursorIndex > 4)
			{
				m_nCursorIndex = 0;
			}
			else
			{
				++m_nCursorIndex;
			}
		}
		else if(pDI->KeyPressed(DIK_LEFT) ||  pDI->JoystickDPadPressed(DIR_LEFT))
		{
			if(m_nCursorIndex == 0)
			{
				m_nCursorIndex = 5;
			}
			else
			{
				--m_nCursorIndex;
			}
		}
		if(pDI->KeyPressed(DIK_RETURN) || pDI->JoystickButtonPressed(1) )
		{
			if( m_nCursorIndex == 0 )
				CGame::GetInstance()->ChangeState(CGamePlayState::GetInstance());
			else if( m_nCursorIndex == 1 )
			{
				CGame::GetInstance()->ChangeState(COptionsState::GetInstance());
			}
			else if( m_nCursorIndex == 2 )
			{
				CGame::GetInstance()->ChangeState(CHowToPlay::GetInstance());
			}
			else if( m_nCursorIndex == 4 )
			{
				CGame::GetInstance()->ChangeState(CCreditsState::GetInstance());			
			}
			else if( m_nCursorIndex == 5 )
			{
				return false;
			}

		}
	}
	else
	{
		if(pDI->KeyPressed(DIK_RETURN) || pDI->JoystickButtonPressed(9) || pDI->JoystickButtonPressed(1) )
		{
			m_bPressStart = false;
		}
	}
	return true;
}

void CMainMenuState::Update(float _fDT)
{

	//CSGD_FModManager::GetInstance()->Update();
	if( m_nScroll > CGame::GetInstance()->GetScnWidth() *.5 - ( 352*m_nCursorIndex) ||  m_nScroll < CGame::GetInstance()->GetScnWidth() *.5 - ( 352*m_nCursorIndex)   )
	{
		if( m_nScroll < CGame::GetInstance()->GetScnWidth() *.5+ - ( 352*m_nCursorIndex))
		{
			m_nScroll+=2;
		}
		else if( m_nScroll > CGame::GetInstance()->GetScnWidth() *.5 - ( 352*m_nCursorIndex))
		{
			m_nScroll-=2;
		}

	}

	for( int i = 0; i < 6; i++ )
	{
		m_nAlphaChange[i] = 75;

	}
	m_nAlphaChange[m_nCursorIndex] = 255;

	if( m_bPressStart )
	{
		static bool ChangeAlpha = false;
		static DWORD dTimeStamp2 = GetTickCount();

		if(GetTickCount() - dTimeStamp2 > 400 )
		{
			if(!ChangeAlpha )
			{
				while( m_nAlphaStart <= 255 )
				{
				
					m_nAlphaStart+=1;
				}
				ChangeAlpha = true;
			}
			else
			{
				while( m_nAlphaStart >= 0 )
				{
				
					m_nAlphaStart-=1;
				}
				ChangeAlpha = false;
			}
			dTimeStamp2 = GetTickCount();
		}
	}

}

void CMainMenuState::Render(void)
{
	CSGD_Direct3D* pD3D = CSGD_Direct3D::GetInstance();
	CSGD_TextureManager* pTM = CSGD_TextureManager::GetInstance();
	pD3D->Clear(10, 3, 3);
	pD3D->DeviceBegin();
	pD3D->SpriteBegin();

	pTM->Draw(_nBKGGImage, 0, 344);
	pTM->Draw(_nBKGGImage, 256, 344);
	pTM->Draw(_nBKGGImage, 512, 344);
	pTM->Draw(_nBKGGImage, 768, 344);

	CBitmapFont bitText(32,32,16,8);
	RECT rTitle;
	rTitle.bottom = 233;
	rTitle.left = 0;
	rTitle.top = 0;
	rTitle.right = 800;

	pTM->Draw(m_nLogo, 0,0,1.0f,1.0f,&rTitle);
	bitText.SetImageID(pTM->LoadTexture("resource/DHD_Font_Bitmap.png"));

	//bitText.Draw("Grunge Punch Baton Munch",	130, 10,.750f,D3DCOLOR_XRGB(255,255,255)); 

	 
	RECT Screen = {CGame::GetInstance()->GetScnHeight(), 0, CGame::GetInstance()->GetScnWidth(), 50 };
	
	Screen.bottom = (CGame::GetInstance()->GetScnHeight() - 100);
	Screen.left = 0;
	Screen.right = (CGame::GetInstance()->GetScnWidth() );
	Screen.top = CGame::GetInstance()->GetScnHeight()*.75;

	pD3D->DrawRect( Screen, 10 , 0, 0, 0 );

#if 1
	// Kerning for bitmap text
	if( m_bPressStart)
	{
		bitText.Draw("P",	m_nScroll-68,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB( m_nAlphaStart, 255, 255,255)); 
		bitText.Draw("r",	m_nScroll-56,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB( m_nAlphaStart, 255, 255,255)); 
		bitText.Draw("e",	m_nScroll-43,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB( m_nAlphaStart, 255, 255,255)); 
		bitText.Draw("s",	m_nScroll-31,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB( m_nAlphaStart, 255, 255,255)); 
		bitText.Draw("s",	m_nScroll-18,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB( m_nAlphaStart, 255, 255,255)); 

		bitText.Draw("S",	m_nScroll+7,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB( m_nAlphaStart, 255, 255,255)); 
		bitText.Draw("t",	m_nScroll+19,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB( m_nAlphaStart, 255, 255,255)); 
		bitText.Draw("a",	m_nScroll+31,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB( m_nAlphaStart, 255, 255,255)); 
		bitText.Draw("r",	m_nScroll+43,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB( m_nAlphaStart, 255, 255,255)); 
		bitText.Draw("t",	m_nScroll+55,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB( m_nAlphaStart, 255, 255,255)); 

	}
	else
	{
	bitText.Draw("G",	m_nScroll-68,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[0], 255, 255,255)); 
	bitText.Draw("A",	m_nScroll-56, CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[0],255, 255,255)); 
	bitText.Draw("M",	m_nScroll-43, CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[0],255, 255,255)); 
	bitText.Draw("E",	m_nScroll-31, CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[0], 255, 255,255)); 
	bitText.Draw("M",	m_nScroll-6, CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[0], 255, 255,255)); 
	bitText.Draw("O",	m_nScroll+6, CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[0], 255, 255,255)); 
	bitText.Draw("D",	m_nScroll+19, CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[0], 255, 255,255)); 
	bitText.Draw("E",	m_nScroll+31, CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[0], 255, 255,255)); 

	bitText.Draw("O",		m_nScroll+296,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[1],255,255,255)); 
	bitText.Draw("P",		m_nScroll+309,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[1],255,255,255)); 
	bitText.Draw("T",		m_nScroll+322,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[1],255,255,255)); 
	bitText.Draw("I",		m_nScroll+334,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[1],255,255,255)); 
	bitText.Draw("O",		m_nScroll+346,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[1],255,255,255)); 
	bitText.Draw("N",		m_nScroll+359,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[1],255,255,255)); 
	bitText.Draw("S",		m_nScroll+372,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[1],255,255,255)); 


	bitText.Draw("H",	m_nScroll+626,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[2],255,255,255)); 
	bitText.Draw("O",	m_nScroll+639,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[2],255,255,255)); 
	bitText.Draw("W",	m_nScroll+652,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[2],255,255,255)); 
	bitText.Draw("T",	m_nScroll+677,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[2],255,255,255)); 
	bitText.Draw("O",	m_nScroll+690,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[2],255,255,255)); 
	bitText.Draw("P",	m_nScroll+713,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[2],255,255,255)); 
	bitText.Draw("L",	m_nScroll+726,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[2],255,255,255)); 
	bitText.Draw("A",	m_nScroll+738,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[2],255,255,255)); 
	bitText.Draw("Y",	m_nScroll+749,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[2],255,255,255)); 

	//bitText.Draw("HOW-TO-PLAY",	m_nScroll+576,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[2],255,255,255)); 

	//bitText.Draw("HIGHSCORES",	m_nScroll+928,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[3],255,255,255)); 
	bitText.Draw("H",	m_nScroll+983,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[3],255,255,255)); 
	bitText.Draw("I",	m_nScroll+996,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[3],255,255,255)); 
	bitText.Draw("G",	m_nScroll+1008,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[3],255,255,255)); 
	bitText.Draw("H",	m_nScroll+1022,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[3],255,255,255)); 
	bitText.Draw("S",	m_nScroll+1036,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[3],255,255,255)); 
	bitText.Draw("C",	m_nScroll+1049,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[3],255,255,255)); 
	bitText.Draw("O",	m_nScroll+1061,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[3],255,255,255)); 
	bitText.Draw("R",	m_nScroll+1074,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[3],255,255,255)); 
	bitText.Draw("E",	m_nScroll+1087,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[3],255,255,255)); 
	bitText.Draw("S",	m_nScroll+1099,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[3],255,255,255)); 

//	bitText.Draw("CREDITS",		m_nScroll+1312,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[4],255,255,255)); 
	bitText.Draw("C",		m_nScroll+1352,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[4],255,255,255)); 
	bitText.Draw("R",		m_nScroll+1365,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[4],255,255,255)); 
	bitText.Draw("E",		m_nScroll+1377,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[4],255,255,255)); 
	bitText.Draw("D",		m_nScroll+1390,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[4],255,255,255)); 
	bitText.Draw("I",		m_nScroll+1401,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[4],255,255,255)); 
	bitText.Draw("T",		m_nScroll+1413,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[4],255,255,255)); 
	bitText.Draw("S",		m_nScroll+1425,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[4],255,255,255)); 

	//bitText.Draw("EXIT",		m_nScroll+1696,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[5],255,255,255)); 
	bitText.Draw("E",		m_nScroll+1736,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[5],255,255,255)); 
	bitText.Draw("X",		m_nScroll+1749,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[5],255,255,255)); 
	bitText.Draw("I",		m_nScroll+1761,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[5],255,255,255)); 
	bitText.Draw("T",		m_nScroll+1773,CGame::GetInstance()->GetScnHeight()*.75,.75f,D3DCOLOR_ARGB(m_nAlphaChange[5],255,255,255)); 
	}

#endif 

	pD3D->GetSprite()->Flush();


	pD3D->DeviceEnd();
	pD3D->SpriteEnd();



	pD3D->Present();

}

void CMainMenuState::Exit(void)
{


	
}

CMainMenuState::~CMainMenuState()
{
}